<template>
  <div>
    <div class="header_search_nav search_form">
      <div class="ipt_left">
        <el-form
          ref="queryParams"
          :model="queryParams"
          label-width="130px"
          @submit.native.prevent
        >
          <el-row :gutter="24">
            <el-col :span="5">
              <el-form-item label="等级名称" prop="thLvRuName">
                <el-input
                  v-model="queryParams.thLvRuName"
                  placeholder="请输入等级名称"
                  class="w-100"
                  @keyup.enter.native="handleQuery"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>

      <div class="btn_right">
        <button class="btn1" @click="handleQuery">筛选</button>
        <button class="btn2" @click="resetQuery">重置</button>
      </div>
    </div>

    <div class="table_card">
      <div class="table_card_top">
        <span class="solid_left"></span>
        <div class="table_card_title">技师等级规则</div>
        <div class="op_right">
          <button class="primary_button" @click="handleAdd">
            添加等级规则
          </button>
        </div>
      </div>

      <div class="table_list">
        <el-table :data="tableData" stripe style="width: 100%">
          <el-table-column prop="thLvRuName" label="等级名称"></el-table-column>
          <el-table-column prop="thLvRuDo" label="积分倍率"></el-table-column>
          <el-table-column
            prop="thLvRuPo"
            label="成长值最小值"
          ></el-table-column>
          <el-table-column prop="thLvRuDay" label="等级有效期">
            <template slot-scope="scope">
              <div v-if="scope.row.thLvRuStatus === 'E'">
                {{ scope.row.thLvRuDay }}天
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="thLvRuStatus" label="等级有效期限制">
            <template slot-scope="scope">
              {{ thLvRuStatusObj[scope.row.thLvRuStatus] }}
            </template>
          </el-table-column>
          <el-table-column prop="thLvRuReDay" label="注册天数">
            <template slot-scope="scope">
              <div v-if="scope.row.thLvRuReStatus === 'Y'">
                {{ scope.row.thLvRuReDay }}天
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="thLvRuReStatus" label="注册天数限制类型">
            <template slot-scope="scope">
              {{ thLvRuReStatusObj[scope.row.thLvRuReStatus] }}
            </template></el-table-column
          >
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button size="mini" type="text" @click="edit(scope.row)"
                >编辑</el-button
              >
              <el-button size="mini" type="text" @click="dele(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <pagination
          :total="total"
          :page.sync="queryParams.pageNo"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>

      <!-- 新增技师等级规则 -->
      <el-dialog
        :visible.sync="StoreLvRuledialogVisible"
        width="500px"
        :title="title"
        class="no-dialog-padding"
        :before-close="handleClose"
      >
        <span>
          <div class="AddRules_box">
            <el-form
              ref="StoreLvRuleForm"
              :rules="AddRules"
              :model="StoreLvRuleForm"
              label-position="top"
            >
              <el-row>
                <el-form-item label="等级名称" prop="thLvRuName">
                  <el-input
                    v-model="StoreLvRuleForm.thLvRuName"
                    placeholder="请输入"
                  ></el-input>
                </el-form-item>
              </el-row>
              <el-row>
                <el-form-item label="等级有效期限制" prop="thLvRuStatus">
                  <el-select
                    v-model="StoreLvRuleForm.thLvRuStatus"
                    placeholder="请选择"
                    filterable
                    clearable
                    class="w-100"
                  >
                    <el-option
                      v-for="(item, index) in thLvRuStatusOptions"
                      :key="index"
                      :label="item.label"
                      :value="item.value"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-row>
              <el-row>
                <el-form-item
                  label="等级有效期"
                  prop="thLvRuDay"
                  v-if="StoreLvRuleForm.thLvRuStatus === 'E'"
                >
                  <el-input-number
                    v-model="StoreLvRuleForm.thLvRuDay"
                    placeholder="请输入"
                    :precision="0"
                    :step="0"
                    :min="0"
                    :controls="false"
                    class="w-100 text-left"
                  ></el-input-number>
                </el-form-item>
              </el-row>
              <el-row>
                <el-form-item label="注册天数限制类型" prop="thLvRuReStatus">
                  <el-select
                    v-model="StoreLvRuleForm.thLvRuReStatus"
                    placeholder="请选择"
                    filterable
                    clearable
                    class="w-100"
                  >
                    <el-option
                      v-for="(item, index) in thLvRuReStatusOptions"
                      :key="index"
                      :label="item.label"
                      :value="item.value"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-row>
              <el-row>
                <el-col>
                  <el-form-item
                    label="注册天数"
                    prop="thLvRuReDay"
                    v-if="StoreLvRuleForm.thLvRuReStatus === 'Y'"
                  >
                    <el-input-number
                      v-model="StoreLvRuleForm.thLvRuReDay"
                      placeholder="请输入"
                      :precision="0"
                      :step="0"
                      :min="0"
                      :controls="false"
                      class="w-100 text-left"
                    ></el-input-number>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-form-item label="积分倍率" prop="thLvRuDo">
                  <el-input-number
                    v-model="StoreLvRuleForm.thLvRuDo"
                    placeholder="请输入"
                    :precision="0"
                    :step="0"
                    :min="0"
                    :controls="false"
                    class="w-100 text-left"
                  ></el-input-number>
                </el-form-item>
              </el-row>
              <el-row>
                <el-col>
                  <el-form-item label="成长最小值" prop="thLvRuPo">
                    <el-input-number
                      v-model="StoreLvRuleForm.thLvRuPo"
                      placeholder="请输入"
                      :controls="false"
                      :precision="0"
                      :step="0"
                      :min="0"
                      class="w-100 text-left"
                    ></el-input-number>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </div>
        </span>
        <span slot="footer" class="dialog-footer">
          <el-button class="cancel_btn" @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="AddRulesSubmit">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  getStoreLvRulePageApi,
  AddStoreLvRulePageApi,
  UpdateStoreLvRulePageApi,
  getStoreLvRulePageInfoApi,
  DelStoreLvRulePageApi,
} from "@/api/bpm/rankRules";
export default {
  name: "EryuoaStoreAddrules",

  data() {
    return {
      StoreLvRuledialogVisible: false,
      loading: false,
      activeIndex: 1,
      currentIndex: 1,
      tableData: [],
      StoreSlist: [],
      title: "",
      value: "",
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        thLvRuName: "",
      },

      thLvRuStatusOptions: [
        {
          value: "E",
          label: "限时有效",
        },
        {
          value: "F",
          label: "永久有效",
        },
      ],

      // E:限时有效;F:永久有效)
      thLvRuStatusObj: {
        E: "限时有效",
        F: "永久有效",
      },

      thLvRuReStatusOptions: [
        {
          value: "N",
          label: "不限制",
        },
        {
          value: "Y",
          label: "限制",
        },
      ],

      //N:不限制;Y:限制
      thLvRuReStatusObj: {
        N: "不限制",
        Y: "限制",
      },

      total: 0,

      AddRules: {
        thLvRuName: [
          { required: true, message: "请输入等级名称", trigger: "blur" },
        ],
        thLvRuDo: [{ required: true, message: "请输入", trigger: "blur" }],
        thLvRuStatus: [
          { required: true, message: "请选择", trigger: "change" },
        ],
        thLvRuReStatus: [
          { required: true, message: "请选择", trigger: "change" },
        ],
        thLvRuReDay: [{ required: true, message: "请输入", trigger: "change" }],
        thLvRuDay: [{ required: true, message: "请输入", trigger: "change" }],
      },

      StoreLvRuleForm: {
        thLvRuName: "",
        thLvRuDay: "",
        thLvRuDo: "",
        thLvRuPo: "",
        thLvRuReDay: "",
        thLvRuReStatus: "",
        thLvRuStatus: "",
        thLvRuId: "",
      },
    };
  },

  created() {
    this.getList();
  },

  mounted() {},

  methods: {
    // 获取详情
    getStoreLvRuleInfo(row) {
      let id = row.thLvRuId;
      getStoreLvRulePageInfoApi({ id: id })
        .then((res) => {
          const { code, data } = res;
          if (code === 0) {
            let {
              thLvRuName,
              thLvRuDay,
              thLvRuDo,
              thLvRuPo,
              thLvRuReDay,
              thLvRuReStatus,
              thLvRuStatus,
              thLvRuId, // 技师积分规则标识
            } = data;

            this.StoreLvRuleForm = {
              thLvRuName: thLvRuName,
              thLvRuDay: thLvRuDay,
              thLvRuDo: thLvRuDo,
              thLvRuPo: thLvRuPo,
              thLvRuReDay: thLvRuReDay,
              thLvRuReStatus: thLvRuReStatus,
              thLvRuStatus: thLvRuStatus,
              thLvRuId: thLvRuId, // 技师积分规则标识
            };
          }
        })
        .catch((err) => {});
    },

    resQ() {
      this.queryParams.pageNo = 1;
      this.total = 0;
      this.tableData = [];
      this.getList();
    },
    handleClose() {
      this.resetFormData("StoreLvRuleForm");
      this.StoreLvRuleForm.confId = "";
      this.$nextTick(() => {
        this.StoreLvRuledialogVisible = false;
      });
    },

    // 表单重置
    resetFormData(formName) {
      // console.log(this.$refs[formName]);
      this.$refs[formName].resetFields();
    },

    // 确定
    AddRulesSubmit() {
      this.$refs["StoreLvRuleForm"].validate((valid) => {
        if (valid) {
          if (!this.StoreLvRuleForm.thLvRuId) {
            this.AddStoreLvRule();
          } else {
            this.EditStoreLvRule();
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    // 新建
    AddStoreLvRule() {
      AddStoreLvRulePageApi(this.StoreLvRuleForm)
        .then((res) => {
          const { code } = res;
          if (code === 0) {
            this.getList();
            this.$message({
              type: "success",
              message: "操作成功!",
            });
            this.handleClose();
          }
        })
        .catch((err) => {});
    },

    // 编辑
    EditStoreLvRule() {
      const {
        thLvRuName,
        thLvRuDay,
        thLvRuDo,
        thLvRuPo,
        thLvRuReDay,
        thLvRuReStatus,
        thLvRuStatus,
        thLvRuId,
      } = this.StoreLvRuleForm;
      UpdateStoreLvRulePageApi({
        thLvRuName: thLvRuName,
        thLvRuDay: thLvRuDay,
        thLvRuDo: thLvRuDo,
        thLvRuPo: thLvRuPo,
        thLvRuReDay: thLvRuReDay,
        thLvRuReStatus: thLvRuReStatus,
        thLvRuStatus: thLvRuStatus,
        thLvRuId: thLvRuId, // 技师积分规则标识
      })
        .then((res) => {
          const { code } = res;
          if (code === 0) {
            this.getList();
            this.$message({
              type: "success",
              message: "操作成功!",
            });
            this.handleClose();
          }
        })
        .catch((err) => {});
    },

    getList() {
      this.loading = true;
      getStoreLvRulePageApi(this.queryParams)
        .then((res) => {
          console.log(res);
          this.loading = false;
          const { code, data } = res;
          if (code === 0) {
            this.tableData = data.list;
            this.total = data.total;
            if (data.list.length === 0 && this.queryParams.pageNo > 1) {
              this.queryParams.pageNo = this.queryParams.pageNo - 1;
              this.getList();
            }
          }
        })
        .catch((err) => {
          this.loading = false;
        });
    },

    // 筛选
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetFormData("queryParams");
      this.handleQuery();
    },

    // 表单重置
    resetFormData(formName) {
      console.log(this.$refs[formName]);
      this.$refs[formName].resetFields();
    },

    // 列表 删除
    dele(row) {
      this.$confirm("是否确认删除该条技师等级规则数据?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          DelStoreLvRulePageApi({ id: row.thLvRuId }).then((res) => {
            const { code } = res;
            if (code === 0) {
              this.getList();
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            }
          });
        })
        .catch(() => {});
    },

    handleAdd() {
      this.StoreLvRuledialogVisible = true;
      this.StoreLvRuleForm.thLvRuId = "";
      this.title = "添加技师等级规则";
    },

    edit(row) {
      this.StoreLvRuledialogVisible = true;
      this.title = "修改技师等级规则";
      this.getStoreLvRuleInfo(row);
    },
  },
};
</script>

<style lang="scss" scoped>
.w-200 {
  width: 200px;
  background: #ffffff;
  border-radius: 4px;
}

.client_name {
  font-size: 14px;
  font-weight: 400;
  color: #0052d9;
}

.give {
  height: 40px;
  font-size: 14px;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.85);
  margin-top: 40px;
  line-height: 40px;
}

.el-form-item {
  margin-bottom: 20px;
}

.recharge_form {
  padding: 10px 24px;
}

.memberName {
  font-size: 14px;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.85);
}
.el_row_item {
  margin-bottom: 10px;
}
.el_row_date {
  margin-top: 20px;
}
.el_row_reason {
  margin-top: 10px;
}

.el_row_date {
  .el-form-item {
    margin-bottom: 0px;
  }
}

.separate_left {
  width: 1px;
  height: 14px;
  // background: #e9e9e9;
  margin-right: 12px;
}
.separate_right {
  width: 1px;
  height: 14px;
  // background: #e9e9e9;
  margin-left: 12px;
}

.check {
  font-size: 12px;
  color: #0052d9;
}

.list_Stores {
  height: 22px;
  font-size: 14px;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.9);
  line-height: 22px;
  .list_Stores_item {
    height: 46px;
    line-height: 46px;
    background: #ffffff;
    padding-left: 47px;
    border-bottom: 1px solid #e7e7e7;
  }
}

.AddRules_box {
  padding: 0 24px;
  .Rules_form_box {
    display: flex;
    .Rules_form_select {
      margin-right: 10px;
    }
  }
}

.radio_box {
  display: flex;
  margin-bottom: 10px;
}

.up_ipt {
  width: 300px;
  margin-right: 20px;
}
.down_ipt {
  width: 300px;
  margin-left: 10px;
}
.percentIcon {
  position: absolute;
  top: 55px;
  right: -15px;
}
</style>
